package com.tianruan.dc.modules.sys.user.api;

import com.tianruan.dc.common.constant.GlobalExceptionEnum;
import com.tianruan.dc.common.utils.response.VResponse;
import com.tianruan.dc.modules.sys.user.entity.db.UserDO;
import com.tianruan.dc.modules.sys.user.entity.dto.LoginDTO;
import com.tianruan.dc.modules.sys.user.entity.dto.UserDTO;
import com.tianruan.dc.modules.sys.user.entity.vo.LoginVO;
import com.tianruan.dc.modules.sys.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

/**
 * @PS
 */
@Slf4j
@RestController
public class LoginController {

    @Autowired(required = false)
    private TokenStore redisTokenStore;

    /**
     * 退出登录，清除token
     *
     * @param principal
     * @param request
     * @param response
     */
    @PostMapping("/oauth/logout")
    public VResponse<Object> logout(Principal principal,
                                    HttpServletRequest request,
                                    HttpServletResponse response) {
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) principal;
        OAuth2AccessToken accessToken = redisTokenStore.getAccessToken(oAuth2Authentication);
        redisTokenStore.removeAccessToken(accessToken);
        redisTokenStore.removeAccessTokenUsingRefreshToken(accessToken.getRefreshToken());
        redisTokenStore.removeRefreshToken(accessToken.getRefreshToken());
        return VResponse.result(GlobalExceptionEnum.LOGOUT_SUCCESS);
    }
}

//
//    /**
//     * 登录
//     * @param loginDTO
//     * @return
//     */
//    @Qualifier("userService")
//    @Autowired
//    private IUserService iUserService;
//    @Autowired
//    private JwtProperties jwtProperties;
//    @PostMapping("/oauth/login")
//    public VResponse<LoginVO> login(@RequestBody LoginDTO loginDTO){
//        log.info("登录：{}",loginDTO);
////        String username = userDO.getUsername();
////        String password = userDO.getPassword();
////        Boolean user = iUserService.equalsPassword(username, password);
////        if (username == null) {
////            //账号不存在
////            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
////        }
//
//        UserDTO userDTO = iUserService.login(loginDTO);
//
//        //登录成功后，生成jwt令牌
//        Map<String, Object> claims = new HashMap<>();
//        claims.put("ID", userDTO.getId());
//        String token = JwtUtil.createJWT(
//                jwtProperties.getAdminSecretKey(),
//                jwtProperties.getAdminTtl(),
//                claims);
//
//        LoginVO loginVO = LoginVO.builder()
//                .id(Long.valueOf(userDTO.getId()))
//                .userName(userDTO.getUsername())
//                .name(userDTO.getName())
//                .token(token)
//                .build();
//
//        return VResponse.success(loginVO);
//    }
//}
